package com.topvision.common.excel;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author shengwm
 * @version V0.0.1
 * @Title: ExcelWriter
 * @Package com.topvision.common.excel
 * @Description: 适用于mybatis poi导出
 * @date 2017/6/6 18:57
 */
public class ExcelWriter {

    /**
     * 将数据以EXCEL 的形式输出到指定IO设备上
     *
     * @param title   表格标题名
     * @param headers 表格属性列名二维数组{英文列名，中文列头}
     * @param dataset List<Map>集合数据
     * @param out     与输出设备关联的流对象，可以将EXCEL文档导出到本地或者网络中
     */
    @SuppressWarnings("unchecked")
    public void exportExcel(String title, String[][] headers,
                            List<Map<String, Object>> dataset, HttpServletResponse response) {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet(title);
        sheet.setDefaultColumnWidth(15);
        HSSFCellStyle style = workbook.createCellStyle();
        style.setFillForegroundColor(HSSFColor.WHITE.index);
        style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style.setBorderBottom(HSSFCellStyle.BORDER_NONE);
        style.setBorderLeft(HSSFCellStyle.BORDER_NONE);
        style.setBorderRight(HSSFCellStyle.BORDER_NONE);
        style.setBorderTop(HSSFCellStyle.BORDER_NONE);
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        HSSFFont font = workbook.createFont();
        //font.setColor(HSSFColor.VIOLET.index);
        font.setFontHeightInPoints((short) 12);
        font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
        style.setFont(font);
        HSSFCellStyle style2 = workbook.createCellStyle();
        //style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);
        style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
        style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);
        style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);
        style2.setBorderRight(HSSFCellStyle.BORDER_THIN);
        style2.setBorderTop(HSSFCellStyle.BORDER_THIN);
        style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);
        style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
        HSSFFont font2 = workbook.createFont();
        font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);
        style2.setFont(font2);
        HSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            HSSFCell cell = row.createCell(i);
            cell.setCellStyle(style);
            HSSFRichTextString text = new HSSFRichTextString(headers[i][1]);
            cell.setCellValue(text);
        }

        String textValue = null;
        if (null != dataset) {
            Map<String, Object> map = new HashMap<String, Object>();
            for (int i = 0; i < dataset.size(); i++) {
                map = dataset.get(i);
                int index = 0;
                row = sheet.createRow(i + 1);
                for (int j = 0; j < headers.length; j++) {

                    textValue =   (null == map.get(headers[j][0]) ? "" : map.get(headers[j][0]).toString());
                    if (null != textValue) {
                        HSSFCell cell = row.createCell(index++);
                        HSSFRichTextString richString = new HSSFRichTextString(textValue);
                        HSSFFont font3 = workbook.createFont();
                        //font3.setColor(HSSFColor.BLUE.index);
                        richString.applyFont(font3);
                        cell.setCellValue(richString);
                    }
                }
            }
        }

        try {
            response.reset();
            response.setContentType("application/vnd.ms-excel;charset=utf-8");// // 指定文件的保存类型。
            response.setCharacterEncoding("utf-8");
            response.setHeader("content-Disposition", "attachment;filename=" + URLEncoder.encode(title, "iso-8859-1"));
            OutputStream out = response.getOutputStream();
            workbook.write(out);
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
